//常量
var bs =0;
var butonva=0;
var userid=0;
var userdelid=0;
var allpage;
var thispage=1;
var altype=0;
//运行顺序

nemeselect();
ulshow();


// ajks
function backuser(type,name,count,page) {
	$.ajax({
		url: "backuser",
		type: "post",
		dataType: "JSON",
		data: {
            type:type,
            name:name,
            count:count,
            page:page,
		},
		success: function(data) {
		    console.log(data);
			show(data);
		},
		error: function(err) {
			console.log(err)
		},
	});
}
//修改状态
function upstate(type,roleseq,page,stateid) {
    $.ajax({
        url: "backuser/state",
        type: "post",
        dataType: "JSON",
        data: {
            type:type,
            page:page,
            userid:roleseq,
            stateid:stateid,
        },
        success: function(data) {
            console.log(data);
            show(data);
        },
        error: function(err) {
            console.log(err)
        },
    });
}

function show(data){

    var a =document.getElementById("tbody").innerHTML='';
    allpage=data.pages;
    for(var i=0;i<data.list.length;i++){
        createtable(i,data.list[i].backcount,data.list[i].name,data.list[i].roleName,data.list[i].stateName,data.list[i].backseq);
    }
}

function createtable(id,count,name,role,stat,seq){
    bs =seq;
    var a =document.getElementById("tbody");

    var tr =document.createElement('tr');

    var td =document.createElement('td');
    td.id='seq'+id;
    td.innerHTML=seq;

    var td1 =document.createElement('td');
    td1.innerHTML=count;
    td1.id="td1"+id;

    var td2 =document.createElement('td');
    td2.innerHTML=name;
    td2.id="td2"+id;

    var td3 =document.createElement('td');
    td3.innerHTML=role;

    var td4 =document.createElement('td');
    td4.innerHTML=stat;

    var td5 =document.createElement('td');

    var debu =document.createElement('input');
    debu.type='button';
    debu.class='btn btn-primary btn-sm';
    debu.value="编辑";
    debu.onclick = function(){
        document.getElementById("name").value=document.getElementById(td2.id).innerHTML;
        document.getElementById("count").value=document.getElementById(td1.id).innerHTML;
        userid =document.getElementById(td.id).innerHTML;
        $('#upshow').modal('toggle', 'center');
    }

    var deletebtn =document.createElement('input');
    deletebtn.type='button';
    deletebtn.class='btn btn-primary btn-sm';
    deletebtn.value="删除";
    deletebtn.onclick = function(){
        var id1= document.getElementById(td.id).innerHTML;
        userdelid= document.getElementById(td.id).innerHTML;
        $('#delete').modal('toggle', 'center');
    }

    var jy =document.createElement('input');
    jy.type='button';
    jy.class='btn btn-primary btn-sm';
    jy.id="jy"+seq;
    if(stat=="停用"){
        jy.value="启用";
        butonva=1;
    }else{
         jy.value="停用";
        butonva=2;
    }

    jy.onclick = function(){
        var seq1= document.getElementById(td.id).innerHTML;
        if(document.getElementById( jy.id).value=='停用') {
            butonva="2";
        }else{
            butonva='1';
        }
        upstate("jy",seq1,thispage,butonva);
    }

    var uppewd =document.createElement('input');
    uppewd.type='button';
    uppewd.class='btn btn-primary btn-sm';
    uppewd.value="重置密码";
    uppewd.onclick = function(){

       var id1= document.getElementById(td.id).innerHTML;
        bs =id1;
        $('#uppwd').modal('toggle', 'center');
        // document.getElementById("uppwd").style.display="block";
    }

    tr.appendChild(td);
    tr.appendChild(td1);
    tr.appendChild(td2);
    tr.appendChild(td3);
    tr.appendChild(td4);
    tr.appendChild(td5);
    td5.appendChild(debu);
    td5.appendChild(deletebtn);
    td5.appendChild(jy);
    td5.appendChild(uppewd);
    a.appendChild(tr);
}

//菜单发送
function ulshow(){
    $.ajax({
        url:"ul",
        type:"Post",
        dataType: "json",
        success:function (data){
            showul(data);
            console.log(data);
        },
        error:function (err){
            console.log("进入错误")
        }
    });
}

function showul(data){
    for(var i=0;i<data.length;i++){
        createselect(data[i].roleName,data[i].roleId);
        upcreat(data[i].roleName,data[i].roleId);
    }
}

//创建下拉框
function createselect(name,seq){
    var select =document.getElementById("powername");
    optionid=seq;
    var option =document.createElement("option");
    option.innerHTML=name;
    option.value=seq;
    select.appendChild(option);
}

//创建下拉框
function upcreat(name,seq){
    var select =document.getElementById("uppower");
    optionid=seq;
    var option =document.createElement("option");
    option.innerHTML=name;
    option.value=seq;
    select.appendChild(option);
}


function Arcalefron(){
    if(thispage-1>0){
        thispage=(thispage-1);
        nemeselect();
    }else {
        alert("没有上一页");
    }
}
function Arcalefronnext() {
    if (thispage < allpage) {
        thispage = (thispage + 1);
        nemeselect();
    } else {
       alert("没有下一页");
    }
}

function update(type,name,count,roleid,page,password,userid) {
    $.ajax({
        url: "backuser/edit",
        type: "post",
        dataType: "JSON",
        data: {
            type:type,
            name:name,
            count:count,
            roleid:roleid,
            page:page,
            password:password,
            userid:userid,
        },
        success: function(data) {
            console.log(data);
            show(data);
        },
        error: function(err) {
            console.log(err)
        },
    });
}


function  bj(){
    $('#upshow').modal('hide', 'fit')
    var name =document.getElementById("name").value;
    var count =document.getElementById("count").value;

    var kd1 = document.getElementById('uppower');
    var d1 = kd1.selectedIndex;
    var idnex1 = kd1.options[d1].value;

    update("edit",name,count,idnex1,thispage,"",userid);
}

function updatepassword(type,roleid,password,page) {
    $.ajax({
        url: "backuser/edit",
        type: "post",
        dataType: "JSON",
        data: {
            type:type,
            userid:roleid,
            page:page,
            password:password,
        },
        success: function(data) {
            console.log(data);
            show(data);
        },
        error: function(err) {
            console.log(err)
        },
    });
}
//重置密码ajks
function uppassword(type,roleid,password,page) {
    $.ajax({
        url: "backuser/uppassword",
        type: "post",
        dataType: "JSON",
        data: {
            type:type,
            userid:roleid,
            page:page,
            password:password,
        },
        success: function(data) {
            console.log(data);
            show(data);
        },
        error: function(err) {
            console.log(err)
        },
    });
}
// 重置密码
function upde(){
    var pwdw =document.getElementById("pwd").value;
    uppassword("uppassword",bs,pwdw,thispage);

    $('#uppwd').modal('hide', 'fit');
    var pwdw =document.getElementById("pwd").value="";
}

//用户添加ajks
function useradd(type,count,password,username,roleid,page) {
    $.ajax({
        url: "backuser/insert",
        type: "post",
        dataType: "JSON",
        data: {
            type:type,
            count:count,
            password:password,
            name:username,
            roleid:roleid,
            page:page,
        },
        success: function(data) {
            console.log(data);
            show(data);
        },
        error: function(err) {
            console.log(err)
        },
    });
}
//删除ajks
function del(type,userid,page) {
    $.ajax({
        url: "backuser/delete",
        type: "post",
        dataType: "JSON",
        data: {
            type:type,
            userid:userid,
            page:page,
        },
        success: function(data) {
            console.log(data);
            show(data);
        },
        error: function(err) {
            console.log(err)
        },
    });
}
//用户添加ajks

function add(){
    var name=document.getElementById("addname").value;
    var count=document.getElementById("addcount").value;
    var pwd =document.getElementById("addpwd").value;

    var kd1 = document.getElementById('powername');
    var d1 = kd1.selectedIndex;
    var idnex1 = kd1.options[d1].value;
    useradd("insert",count,pwd,name,idnex1,thispage);
    // document.getElementById("addshow").style.display='none';
    $('#myModal').modal('hide', 'fit');
    var name=document.getElementById("addname").value='';
    var count=document.getElementById("addcount").value='';
    var pwd =document.getElementById("addpwd").value='';
}


function delte(){
    del("delete",userdelid,thispage)
    $('#delete').modal('hide', 'fit');
}


function nemeselect(){
    var name =document.getElementById("selectname").value;
    backuser("notype",name,"count",thispage);
}


